package com.hxzy.system.service.impl;



import com.hxzy.system.mapper.MyBatisBaseDao;
import com.hxzy.system.service.CommonService;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * 类说明
 *
 * @author admin
 * @date 2021-09-01
 */
public class CommonServiceImpl<Model, PK extends Serializable> implements CommonService<Model,PK> {

    //多态
    private MyBatisBaseDao myBatisBaseDao;

    /**
     * 预留set方法，让子类赋值
     * @param myBatisBaseDao
     */
    public void setMyBatisBaseDao(MyBatisBaseDao myBatisBaseDao) {
        this.myBatisBaseDao = myBatisBaseDao;
    }

    @Override
    public int deleteByPrimaryKey(PK id) {

        return this.myBatisBaseDao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Model record) {
        //新增人，新增时间，修改人，修改时间赋值
//        try {
//            Method  createTimeMethod = record.getClass().getMethod("setCreateTime", Date.class);
//            if(createTimeMethod!=null){
//               createTimeMethod.invoke(record, new Date());
//            }
//
//            Method  updateTimeMethod = record.getClass().getMethod("setUpdateTime", Date.class);
//            if(updateTimeMethod!=null){
//                updateTimeMethod.invoke(record, new Date());
//            }
//
//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        }

        return this.myBatisBaseDao.insert(record);
    }





    @Override
    public int insertSelective(Model record) {
        return this.myBatisBaseDao.insertSelective(record);
    }

    @Override
    public Model selectByPrimaryKey(PK id) {
        return (Model) this.myBatisBaseDao.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Model record) {
        return this.myBatisBaseDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Model record) {
        return this.myBatisBaseDao.updateByPrimaryKey(record);
    }
}
